package com.example.administrator.appstpremvpdemo.wegit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.appstpremvpdemo.R;
import com.example.administrator.appstpremvpdemo.helper.PageStateListener;

/**
 * @Author Administrator
 * @Date 2017/12/23
 * @description
 * @function
 */

public class PageStateLayout extends FrameLayout implements PageStateListener {

    private Context mContext;
    private RelativeLayout mNoNetView;
    private RelativeLayout mLoadingView;

    public PageStateLayout(Context context) {
        this(context, null);
    }

    public PageStateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.default_layout, this, true);
        mNoNetView = ((RelativeLayout) findViewById(R.id.rl_no_net));
        mLoadingView = ((RelativeLayout) findViewById(R.id.rl_loading));

        mNoNetView.getChildAt(0).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSetNetErrorRetryListener.setNetErrorRetry();
                Toast.makeText(mContext, "ddd", Toast.LENGTH_SHORT).show();
            }
        });
    }

    int PageState = -1;

    @Override
    public void onLoading() {
        mNoNetView.setVisibility(GONE);
        mLoadingView.setVisibility(VISIBLE);
    }

    @Override
    public void onNetError() {
        mNoNetView.setVisibility(VISIBLE);
        mLoadingView.setVisibility(GONE);
    }

    @Override
    public void onSuccess() {
        mNoNetView.setVisibility(GONE);
        mLoadingView.setVisibility(GONE);
    }

    setNetErrorRetryListener mSetNetErrorRetryListener;

    public void setSetNetErrorRetryListener(setNetErrorRetryListener setNetErrorRetryListener) {
        mSetNetErrorRetryListener = setNetErrorRetryListener;
    }

    public interface setNetErrorRetryListener {
        void setNetErrorRetry();
    }
}
